#include<iostream>
#include<vector>
#include<algorithm>

using std::cin;
using std::cout;
using std::vector;
using std::min;

int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<int> > path_cost(n);
    for (int i = 0; i < n; ++i) {
        path_cost[i].resize(m);
        for (int j = 0; j < m; ++j) {
            cin >> path_cost[i][j];
        }
    }
    for (int i = 1; i < n; ++i) {
        path_cost[i][0] += path_cost[i - 1][0];
    }
    for (int j = 1; j < m; ++j) {
        path_cost[0][j] += path_cost[0][j - 1];
    }
    for (int i = 1; i < n; ++i) {
        for (int j = 1; j < m; ++j) {
            path_cost[i][j]
            += min(path_cost[i - 1][j], path_cost[i][j - 1]);
        }
    }
    cout << path_cost[n - 1][m - 1] << "\n";
    return 0;
}
